#ifndef CUSTOMLIST_H
#define CUSTOMLIST_H

#include <iostream>
#include <cstdlib>

struct List
{
	int n;
	List* pnext;
};

void printCopyright(bool a);
void createList(List**L1,List**L2,bool LICENSE)
{
	printCopyright(LICENSE);
	int c;
	printf("Please enter the array A that you want to merge, and end with any letter.\n");
	List* t1 = NULL,*t2 = NULL,*p=NULL,*tmp=NULL;
	while(scanf("%d",&c))
	{
		p = (List*)malloc(sizeof(List));
		if(p==NULL)
		{
			std::cout << "ERROR: Memory allocation failed" << std::endl;
			exit(0);
		} 
		p->n = c;
		p->pnext = NULL;
		if(t1 == NULL) t1 = p,tmp = p;
		else tmp->pnext = p,tmp = p;
	}
	p=NULL,tmp=NULL;
	getchar();
	printf("Please enter the array B that you want to merge, and end with any letter.\n");
	while(scanf("%d",&c))
	{
		p = (List*)malloc(sizeof(List));
		if(p==NULL)
		{
			std::cout << "ERROR: Memory allocation failed" << std::endl;
			exit(0);
		} 
		p->n = c;
		p->pnext = NULL;
		if(t2 == NULL) t2 = p,tmp = p;
		else tmp->pnext = p,tmp = p;
	}
	*L1 = t1,*L2 = t2;
}

int ListLength(List *head)
{
	List *p = head;
	int cnt = 0;
	while(p!=NULL)
	{
		p = p->pnext;
		cnt++;
	}
	return cnt;
}

void GetElem(List *head,int l,int &elem)
{
	List *p = head;
	for(int i=1;p!=NULL;i++)
	{
		if(i==l) elem = p->n;
		p = p->pnext;
	}
}

void ListInsert(List* head,int len,int elem)
{
	if(len == 1)
	{
		head->n = elem;
		return;
	}
	List *p = head,*tmp = NULL;
	tmp = (List*)malloc(sizeof(List));
	tmp->n = elem;
	if(p==NULL)
	{
		std::cout << "ERROR: Memory allocation failed" << std::endl;
		exit(0);
	}  
	List *pos = NULL;
	for(int i=1;p!=NULL;i++)
	{
		if(i==len-1) 
		{
			pos = p;
			break;
		}
		p = p->pnext;
	}
	tmp->pnext = pos->pnext;
	pos->pnext = tmp;
}

void print(List* head)
{
	std::cout << "The output results are as follows:" << std::endl;
	List *p = head;
	for(int i=1;p!=NULL;i++)
	{
		std::cout << p->n << " ";
		p = p->pnext;
	}
}

bool LocateElem(List *head,int elem,int len)
{
	List *p = head;
	for(int i=1;p!=NULL;i++)
	{
		if(p->n==elem) return true;
		p = p->pnext;
	}
	return false;
}

void printCopyright(bool which)
{
	std::cout << "****************************************************" << std::endl;
	std::cout << "*                COPYRIGHT STATEMENT               *" << std::endl;
	std::cout << "*ONE OF THE PROGRAM'S HEADERS IS CODED BY ZEPHYRBD!*" << std::endl;
	if(which) std::cout << "*       THE PROGRAM IS CODED BY ZEPHYRBD!          *" << std::endl;
	std::cout << "****************************************************" << std::endl;
}
#endif
